package com.nbjtjc.safe.service.edu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduDailyExamineListMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyParticipantMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyRecordMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyUserEvaluateMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.edu.EduDailyParticipant;
import com.nbjtjc.safe.model.edu.EduDailyRecord;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EduDailyRecordService {
	@Autowired
	  private EduDailyRecordMapper eduDailyRecordMapper;
	@Autowired
	  private EduDailyParticipantMapper eduDailyParticipantMapper;
	@Autowired
	  private EduDailyExamineListMapper eduDailyExamineListMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    
	@Autowired
	  private EduDailyUserEvaluateMapper eduDailyUserEvaluateMapper;
	
	  public Result add(EduDailyRecord eduDailyRecord){
		        Result result=new Result();
		        if(ObjectUtil.isNull(eduDailyRecord)
		        		||ObjectUtil.isNull(eduDailyRecord.getInfoId())
		        		){		            
		        	result.setFalse("日常培训信息ID不能为空");
		            return result;
		        }
		        //按录入的infoId在库中查info记录，查出该info记录对应的record
		        int infoId = eduDailyRecord.getInfoId();     
		        EduDailyRecord	record = eduDailyRecordMapper.findByInfoId(infoId);
 
		        //设置参与人员的InfoId为当前record中 的infoId
		        for(EduDailyParticipant part:eduDailyRecord.getEduDailyParticipants()){
		        	part.setEduDailyInfoId(eduDailyRecord.getInfoId());
		        }
		        
		        //关联info对应的record为空为新建，插入数据
		        if(ObjectUtil.isNull(record)){
		        	eduDailyRecord.setCancel(0);
		        	int  companeyId= MyShiroUtils.getCompanyId();
		        	eduDailyRecord.setCompanyId(companeyId);
		        	eduDailyRecord.setCompanyName(sysDeptMapper.findNameById(companeyId));
		        	eduDailyRecord.setCreateDate(new Date());
		        	eduDailyRecord.setCreateId(MyShiroUtils.getSessionUserId());
		        	
		        	int res=eduDailyRecordMapper.insertSelective(eduDailyRecord);
			        if(res==0){
			            result.setFalse("插入日常培训记录失败");
			            return result;
			        }
		        }else
		        	//否则为更新
		        {
		        	
		        	eduDailyRecord.setId(record.getId());
		        	eduDailyRecord.setModifyDate(new Date());
		        	eduDailyRecord.setModifyId(MyShiroUtils.getSessionUserId());
		        	int res=eduDailyRecordMapper.updateByPrimaryKeySelective(eduDailyRecord);
		        	 if(res==0){
		        		 result.setFalse("更新日常培训记录失败");
				         return result;

		        	 }
		        	 //更新参加人员列表
		        	 if(CollectionUtil.isNotEmpty(eduDailyRecord.getEduDailyParticipants())){
		        		 //删除旧的
		        		 res = eduDailyParticipantMapper.deleteByInfoId(eduDailyRecord.getInfoId());
			        	 if(res==0){
			        		 result.setFalse("删除旧参与人员列表失败");
					         return result;
			        	 }
		        		 //插入新的
				      	  result = insertEduDailyParticipant(eduDailyRecord);
				    	  if(result.isSuccess()==false){
				             return result;
				         }
		        	 }
		        	 //更新参加人数
		        	 res = eduDailyRecordMapper.updatePartCount(eduDailyRecord);
		        	 if(res==0){
		        		 result.setFalse("更新参加人数失败");
				         return result;
		        	 }
		        	 //新建用户评价列表
		        	 //如果信息ID名下无用户评价，则新建
		        	 if(CollectionUtil.isEmpty(eduDailyUserEvaluateMapper.findByInfoId(eduDailyRecord.getInfoId()))){
		        		 if(eduDailyParticipantMapper.findParticipantByInfoId(infoId).isEmpty()){
			        		 result.setFalse("实际参与人数为0，无法新建用户评价");
					         return result;
		        		 }
		        		 res= eduDailyUserEvaluateMapper.createUserEvaluateByRecord(eduDailyRecord);
		        		 if(res==0){
			        		 result.setFalse("新建用户评价失败");
					         return result;
			        	 }
		        	 }		        	 
		        }
		        result.setData(eduDailyRecord);

		        return result;
		    }

	    private Result insertEduDailyParticipant(EduDailyRecord eduDailyRecord){
	        Result result=new Result();
	        
	    	List<EduDailyParticipant> eduDailyParticipants = eduDailyRecord.getEduDailyParticipants();
	    	
	    	if(CollectionUtil.isEmpty(eduDailyRecord.getEduDailyParticipants())){
	            result.setFalse("插入对象为空");
	        	return result;
		    }

			for(EduDailyParticipant eduDailyParticipant:eduDailyParticipants){
				eduDailyParticipant.setCancel(0);
			}
	    	int res = eduDailyParticipantMapper.insertList(eduDailyRecord.getEduDailyParticipants());
	    	if(res==0){
	            result.setFalse("新建参与人员列表失败");
	            return result;
	        }
	    	result.setData(eduDailyRecord.getEduDailyParticipants());
	    	return result;
	    }
	  
	  public Result delete(int eduDailyRecordId){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduDailyRecordId)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = eduDailyRecordMapper.deleteByPrimaryKey(eduDailyRecordId);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	
	      }
	      return result;
	  }

	  
	  public Result find(EduDailyRecord eduDailyRecord) {
	      Result result = new Result();
	      PageHelper.startPage(eduDailyRecord.getPageCurrent(), eduDailyRecord.getPageSize());
	      eduDailyRecord.setCompanyId(MyShiroUtils.getCompanyId());

	      List<EduDailyRecord> eduDailyRecords = eduDailyRecordMapper.select(eduDailyRecord);
	      result.setData(new PageInfo<>(eduDailyRecords));

	      return result;
	  }
	  

}
